package giit.computerbase.action;

import giit.computerbase.server.RegisterCodeServer;
import giit.computerbase.server.UserServer;
import giit.computerbase.util.Application;
import giit.computerbase.util.DealWithCharcter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2018-11-29 Thursday 21:20:57
 */
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding((String) Application.getConfig().pageConfig.get("encode"));
        response.setCharacterEncoding((String) Application.getConfig().pageConfig.get("encode"));
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        String again = request.getParameter("again");
        String code = request.getParameter("code");
        String toPath = "warn.jsp?";
        if (user != null && !user.equals("") &&
                password != null && !password.equals("") &&
                again != null && !again.equals("") &&
                code != null && !code.equals("")) {
            user = URLDecoder.decode(user, "utf-8");
            password = URLDecoder.decode(password, "utf-8");
            again = URLDecoder.decode(again, "utf-8");
            code = URLDecoder.decode(code, "utf-8");
            user = DealWithCharcter.dealWith(user);
            password = DealWithCharcter.dealWith(password);
            again = DealWithCharcter.dealWith(again);

            Pattern pattern = Pattern.compile("[a-zA-Z0-9_]{0,}");
            Matcher matcher = pattern.matcher(user);
            if (matcher.find() && matcher.group(0).length() == user.length()) {
                if (password.length() >= 6) {
                    if (password.equals(again)) {
                        RegisterCodeServer codeServer = new RegisterCodeServer();
                        if (codeServer.find(code)) {
                            UserServer userServer = new UserServer();
                            if (userServer.register(user, Application.COMPUTER_BASE_NAME[1], password)) {
                                toPath += ("state=panel-primary&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册成功\r\r用户名：" + user) + URLEncoder.encode("\r\r密码：" + password) + "&preview=home.jsp");
                            } else {
                                toPath += ("state=panel-danger&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册失败，用户名已被占用") + "&preview=register.jsp");
                            }
                            codeServer.deleteCode(code);
                        } else {
                            toPath += ("state=panel-danger&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册失败，注册码无效") + "&preview=register.jsp");
                        }
                    } else {
                        toPath += ("state=panel-danger&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册失败，输入的密码不一致") + "&preview=register.jsp");
                    }
                } else {
                    toPath += ("state=panel-danger&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册失败，密码小于6位") + "&preview=register.jsp");
                }
            } else {
                toPath += ("state=panel-danger&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册失败，用户名包含除字母、数字和下划线之外的字符") + "&preview=register.jsp");
            }
        } else {
            toPath += ("state=panel-danger&title=" + URLEncoder.encode("注册") + "&context=" + URLEncoder.encode("注册失败，注册信息填写不完整") + "&preview=register.jsp");
        }
        response.sendRedirect(toPath);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
